<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

/**
 * 财务逻辑
 */
class Moneyapp extends AdminBase
{

    /**
     * 获取提现信息
     */
    public function getMoneyappInfo($where = [], $field = true)
    {
        
        $info = $this->modelMoneyapp->getInfo($where, $field);
        
        //$info['leader_username'] = $this->modelMoneyapp->getValue(['id' => $info['tjid']], 'username');
        
        return $info;
    }
    /**
     * 导出提现列表
     */
    public function exportMoneyappList($where = [], $field = 'm.*,b.username,b.name', $order = 'm.id desc')
    {
        
        $list = $this->getMoneyappList($where, $field, $order, false);
        
        $titles = "用户名,昵称,提现金额,手续费,实际到账,银行,银行卡号,开户名,开户手机,时间,备注";
        $keys   = "username,name,money,moneyTax,moneyMy,bankName,bank,bankUser,phone,create_time,words";
        
        action_log('导出', '导出提现申请列表');
        
        export_excel($titles, $keys, $list, '提现申请列表');
    }	
    /**
     * 获取提现列表
     */
    public function getMoneyappList($where = [], $field = 'm.*,b.username,b.name', $order = 'm.stat asc,m.id desc', $paginate = DB_LIST_ROWS)
    {
        
        $this->modelMoneyapp->alias('m');
        
        $join = [
                    [SYS_DB_PREFIX . 'user b', 'm.user_id = b.id', 'LEFT'],
                ];
        
        //$where['m.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        $this->modelMoneyapp->join = $join;
        
        return $this->modelMoneyapp->getList($where, $field, $order, $paginate);
    }
	/**
     * 获取提现列表搜索条件
     */
    public function getWhere($data = [],$type='')
    {      
        $where = [];
        !empty($data['search_data']) && $where['b.name|b.username|m.bankUser'] = ['like', '%'.$data['search_data'].'%'];
		(isset($data['stat']) && $data['stat']!=9) && $where['m.stat']=$data['stat'];
        if(isset($data['type']) && $data['type'] != 9){
            $where['m.type']=$data['type'];
        }
        if(!empty($data['start_time']) && empty($data['end_time'])){
            $where['m.create_time']=['>',strtotime($data['start_time'])];
        }
        if(!empty($data['end_time']) && empty($data['start_time'])){
            $where['m.create_time']=['<',strtotime($data['end_time'])];
        }
        if(!empty($data['end_time']) && !empty($data['start_time'])){
            $where['m.create_time']=[['>',strtotime($data['start_time'])],['<',strtotime($data['end_time'])]];
        }
        if(isset($data['is'])){
            $where['m.is']=$data['is'];
        }else{
            if(!empty($type)){
                $where['m.is']=1;
            }else{
                $where['m.is']=0;
            }
        }



        return $where;
    }
	/**
     * 状态设置
     */
    public function updateStat($data)
    {
		$info=db('moneyapp')->where('id',$data['id'])->find();
		if(empty($info) || $info['stat'] != 0){
			return  json_encode(array('error'=>1,'msg'=>'参数错误'));
		}
		$data['op_time']=time();
		$res=db('moneyapp')->where('id',$data['id'])->update($data);
		if($data['stat'] == 2){
			$re=moneyyj_log($info['money'],$info['user_id'],5,$info['user_id']);
		}else{
			$re=true;
            //推送
            jpush(strval($info['user_id']),2,'您的提现申请已转款，请查收！');
		}		
        ($res && $re) && action_log('提现状态', '提现状态操作' . '，id：' . $data['id'] . '，stat：' . $data['stat']);        
        return ($res && $re) ? json_encode(array('error'=>0,'msg'=>'操作成功')) : json_encode(array('error'=>1,'msg'=>'操作失败'));
    }

}
